<script setup>
import {useHomeStore} from '@/stores/home'
import {onMounted, reactive, ref} from "vue";
import {getBaseCategoryList} from "@/api";
import {gotoSearch} from "@/components/TypeNav/func";

let store = useHomeStore();

let navList = store.data.navList
let baseCategoryList = store.data.baseCategoryList;

let categoryList = ref([])
if (baseCategoryList && baseCategoryList.length > 0) {
    categoryList.value = baseCategoryList
} else {
    getBaseCategoryList().then((res) => {
        store.createCategory(res.data)
        categoryList.value = res.data
    })
}


const goSearch = (event) => {
    let {name, first, second, third} = event.target.dataset

    if (name) {
        let params = {
            name: 'search',
            categoryName: name
        }

        if (first) {
            params.category1Id = first
        }
        if (second) {
            params.category2Id = second
        }
        if (third) {
            params.category3Id = third
        }

        gotoSearch(params)

    }
}

const props = defineProps(['show'])

let showed = ref(props.show)

const categoryShow = () => {
    showed.value = !(showed.value)
}

</script>

<template>
  <!-- 商品分类导航 -->
    <div class="type-nav">
        <div class="container">
            <h2 class="all" @mouseenter="categoryShow" @mouseleave="categoryShow">全部商品分类</h2>
            <nav class="nav" v-for="item in navList" :key="item.name">
                <a :href="item.link">{{ item.name }}</a>
            </nav>
            <div class="sort" v-show="showed">
                <div class="all-sort-list2" @click="goSearch">
                    <div class="item" v-for="level1 in categoryList" :key="level1.categoryId">
                        <h3>
                            <a :data-name="level1.categoryName" :data-first="level1.categoryId"> {{
                                level1.categoryName
                                }}</a>
                        </h3>
                        <div class="item-list clearfix">
                            <div class="subitem">
                                <dl class="fore" v-for="level2 in level1.categoryChild" :key="level2.categoryId">
                                    <dt>
                                        <a :data-name="level2.categoryName" :data-second="level2.categoryId">
                                            {{ level2.categoryName }}</a>
                                    </dt>
                                    <dd>
                                        <em v-for="level3 in level2.categoryChild" :key="level3.categoryId">
                                            <a :data-name="level3.categoryName" :data-third="level3.categoryId">
                                                {{ level3.categoryName }}</a>
                                        </em>
                                    </dd>
                                </dl>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            background-color: skyblue;

            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>
